package com.hoyo.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hoyo.HoyoApplication;
import com.hoyo.common.PageInfo;
import com.hoyo.config.UserRealm.Principal;
import com.hoyo.domain.Bullet;
import com.hoyo.domain.Comment;
import com.hoyo.domain.ReportItem;
import com.hoyo.domain.User;
import com.hoyo.service.BulletService;

@Controller
public class BulletController {
	
	@Autowired
	private BulletService bulletService;
	
	
	/*
	 * 发送弹幕
	 * role:user
	 */
	@RequestMapping("/bullet/new")
	@ResponseBody
	public String newBullet(HttpServletRequest req,HttpSession session){
		JSONObject obj = new JSONObject();
		try {
			BufferedReader reader = req.getReader();
			StringBuilder builder = new StringBuilder();
			String buffer = null;
			while((buffer = reader.readLine())!=null){
				builder.append(buffer);
			}
			Bullet bullet = JSONObject.parseObject(builder.toString(), Bullet.class);
			bullet.setUserId(((User)session.getAttribute("user")).getId());
			bulletService.save(bullet);
			//obj.put("danmu",bulletService.findBulletById(bullet.getId()));
			//obj.put("new",true);
			obj.put("code", 1);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("code", 2);
		}
		return obj.toString();
	}
	
	/*
	 * 举报弹幕
	 * role:user
	 */
	@RequestMapping(value="/bullet/report", method=RequestMethod.POST)
	@ResponseBody
	public String reportBullet(@RequestParam("danmu_id") int bulletId, @RequestParam("description") String description,HttpSession session){
		JSONObject obj = new JSONObject();
		try {
			bulletService.report(((User)session.getAttribute("user")).getPhone(), bulletId, description);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	/*
	 * 举报弹幕
	 * role:amdin
	 */
	@RequestMapping("/admin/bullet/beReported")
	@ResponseBody
	public String beReportedBullet(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize") int pageSize){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<ReportItem> pageInfo = bulletService.pageFindReportBullets(pageNum, pageSize);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	@RequestMapping("/admin/bullet/reportHandle")
	@ResponseBody
	public String reportHandle(@RequestParam("id")int id, @RequestParam("userId") int userId
			, @RequestParam("actionType") int actionType){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<ReportItem> pageInfo = bulletService.reportHandle(id, userId, actionType);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	@RequestMapping("/admin/bullet/bullets")
	@ResponseBody
	public String bullets(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize") int pageSize){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Bullet> pageInfo = bulletService.pageFindBullets(pageNum, pageSize);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	

	@RequestMapping("/admin/bullet/login4bid")
	@ResponseBody
	public String login4bid(@RequestParam("userId") int userId, @RequestParam("id") int id){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Bullet> pageInfo = bulletService.login4bid(userId, id);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	@RequestMapping("/admin/bullet/bullet4bid")
	@ResponseBody
	public String bullet4bid(@RequestParam("userId") int userId, @RequestParam("id") int id){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Bullet> pageInfo = bulletService.comment4bid(userId, id);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	@RequestMapping("/admin/bullet/remove")
	@ResponseBody
	public String remove(@RequestParam("id") int id){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Bullet> pageInfo = bulletService.removeOne(id);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	private static String[] types = {"top","bottom","right"};
	
	private static Random rand = new Random();

	@RequestMapping("/hoyollet/quickimport")
	public String quickimport(){
		JSONObject obj = new JSONObject();
		InputStream is = null;
		try{
			is = HoyoApplication.class.getClassLoader().getResourceAsStream("bullet2.xml");
			SAXReader reader = new SAXReader();
			Document doc = reader.read(is);
			Element root = doc.getRootElement();
			Iterator ei = root.elementIterator();
			while(ei.hasNext()){
				Element ele = (Element) ei.next();
				Attribute attr = ele.attribute("p");
				String text = attr.getText();
				System.out.println(text);
//				192.421,1,25,16777215,1490692229,0,adf5dc86,3150392946"
				String[] contents = text.split(",");
				float appearedTime = Float.parseFloat(contents[0]);
				String type = types[rand.nextInt(3)];
				String color = "#ffffff";
				String content = ele.getText();
				Bullet bullet = new Bullet(26, appearedTime, content, color, type);
				System.out.println(bullet);
				bulletService.save(bullet);
			}
			obj.put("success", true);
		}catch(Exception e){
			obj.put("success", false);
			System.out.println(e.getMessage());
		}finally{
			try {
				if(is !=null){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return obj.toString();
	}

}














